package cn.breeze.basic.controller;

import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.vo.LoginPhoneVo;
import cn.breeze.basic.vo.LoginVo;
import cn.breeze.basic.service.ILoginService;
import cn.breeze.basic.vo.WechatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService iLoginService;

    /**
    * @Title: wechat
    * @Description: 微信登录
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/9 19:12
    * @Parameters: [wechatVo]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatVo wechatVo)
    {
        try {
            return iLoginService.wechat(wechatVo);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: binder
    * @Description: 绑定用户
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/10 17:25
    * @Parameters: [wechatVo]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatVo wechatVo)
    {
        try {
            Map<String,Object> map = iLoginService.binder(wechatVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: account
    * @Description: 账号登录
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/7 12:53
    * @Parameters: [shopAuditLog]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo loginVo)
    {
        try {
            Map<String,Object> map = iLoginService.account(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: account
    * @Description: 手机号登录
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/8 15:24
    * @Parameters: [loginVo]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/phone")
    public AjaxResult phone(@RequestBody LoginPhoneVo loginPhoneVo)
    {
        try {
            Map<String,Object> map = iLoginService.phone(loginPhoneVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /** 
    * @Title: logout
    * @Description: 退出登录
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/8 15:23  
    * @Parameters: [token]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token")String token)
    {
        try {
            iLoginService.logout(token);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }
}
